<template>
  <div id="cluereportedit">
    <el-dialog title="人员信息详情" :visible.sync="personDetailVisible" width="80%" top="20px" :close-on-click-modal="false" :close-on-press-escape="false" append-to-body>
      <personInfo :personId='personId' :hidemoredetail="true"></personInfo>
    </el-dialog>
    <div v-show="pageLoading">数据加载中...</div>
    <el-form :model="editForm" label-width="140px" ref="editForm" :inline="true" style="padding:10px 0 0;clear:both" v-show="!pageLoading">
      <el-form-item label="来源" prop="source" :rules="{ required: true, message: '请选择来源', trigger: 'blur' }">
        <div v-if="readOnly" class="details">{{editForm.sourceText}}</div>
        <el-select v-model="editForm.source" placeholder="请选择" style="margin-right:5px;" v-else>
          <el-option v-for="(item, index) in sourceDic.filter(item=>!item.deleted)" :key="index" :label="item.label" :value="item.value"></el-option>
        </el-select><el-button v-if="!readOnly" type="success" size="mini" @click="settingSource" icon="el-icon-setting" circle></el-button>
        <el-dialog title="设置来源" :visible.sync="settingSourceVisible" width="1000px" :close-on-click-modal="false" :close-on-press-escape="false" append-to-body>
          <el-form :model="sourceForm" label-width="100px" ref="sourceForm" :inline="true">
            <el-form-item label="来源" prop="label" :rules="{ required: true, message: '请输入来源', trigger: 'blur' }">
              <el-input v-model="sourceForm.label" style="width:220px"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="addDic1()" type="success">添加</el-button>
            </el-form-item>
          </el-form>
          <el-table :data="sourceDic" highlight-current-row style="width: 100%;" border class="settingDicTable">
            <el-table-column prop="label" label="来源">
            </el-table-column>
            <el-table-column label="状态">
              <template slot-scope="scope">
                <span v-html="dicDeleted(scope.row.deleted)"></span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="300">
              <template slot-scope="scope">
                <el-button @click="setOrder(scope.row.dictOrder,1)" icon="el-icon-arrow-up" type="text" :loading="orderloading">上移</el-button>
                <el-button @click="setOrder(scope.row.dictOrder,2)" icon="el-icon-arrow-down" type="text" :loading="orderloading" style="margin-right:10px;">下移</el-button>
                <el-popconfirm title="确定停用该来源吗？" v-if="!scope.row.deleted" @confirm="stopDic(scope.row.value)">
                  <el-button slot="reference" type="text" icon="el-icon-warning">停用</el-button>
                </el-popconfirm>
                <el-popconfirm title="确定启用该来源吗？" v-if="scope.row.deleted" @confirm="startDic(scope.row.value)">
                  <el-button slot="reference"  type="text" icon="el-icon-success">启用</el-button>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
        </el-dialog>
      </el-form-item>
      <el-form-item label="其他来源" prop="otherSource" v-show="editForm.source==='0108'">
        <div v-if="readOnly" class="details">{{editForm.otherSource}}</div>
        <el-input v-model="editForm.otherSource" style="width:220px" v-else></el-input>
      </el-form-item>
      <div style="clear:both"></div>
      <el-form-item label="文号" prop="fileNumber" :rules="{ required: true, message: '请输入文号', trigger: 'blur' }">
        <div v-if="readOnly" class="details">{{editForm.fileNumber}}</div>
        <el-input v-model="editForm.fileNumber" style="width:220px" v-else></el-input>
      </el-form-item>
      <el-form-item label="登记时间" prop="registerDate" :rules="{ required: true, message: '请选择登记时间', trigger: 'blur' }">
        <div v-if="readOnly" class="details">{{editForm.registerDate}}</div>
        <el-date-picker v-else
          v-model="editForm.registerDate"
          placeholder="请选择登记时间"
          value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <div style="clear:both"></div>
      <el-form-item label="来文标题" prop="title" :rules="{ required: true, message: '请输入来文标题', trigger: 'blur' }">
        <div v-if="readOnly" class="details">{{editForm.title}}</div>
        <el-input v-model="editForm.title" style="width:965px"  v-else></el-input>
      </el-form-item>
      <div style="clear:both"></div>
      <el-form-item label="反映问题分类" prop="caseClass" :rules="{ required: true, message: '请选择反映问题分类', trigger: 'blur' }">
        <div v-if="readOnly" class="details">{{editForm.caseClassText}}</div>
        <el-select v-model="editForm.caseClass" placeholder="请选择" v-else>
          <el-option v-for="(item, index) in caseClassDic" :key="index" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="反映问题分类二" prop="disciplineClass" :rules="{ required: true, message: '请选择反映问题分类二', trigger: 'blur' }">
        <div v-if="readOnly" class="details">{{disciplineClassName(editForm.disciplineClass)}}</div>
        <el-cascader v-model="editForm.disciplineClass" :options="disciplineClassDic1" v-else :props="{ multiple: true }" clearable @change="disciplineClassChange"></el-cascader>

        <el-button v-if="!readOnly" type="success" size="mini" @click="setting" icon="el-icon-setting" circle></el-button>
        <el-dialog title="设置分类" :visible.sync="settingVisible" width="1000px" :close-on-click-modal="false" :close-on-press-escape="false" append-to-body>
          <!-- <table>
            <tr><th>分类名称</th><th>二级分类</th></tr>
            <tbody v-for="(item,index) in this.disciplineClassDic" :key="index">
              <tbody v-if="item.children&&item.children.length>0">
                <tr v-for="(it,idx) in item.children" :key="idx">
                  <td v-if="idx===0" :rowspan="item.children.length">{{item.label}}</td>
                  <td>{{it.label}}</td>
                </tr>
              </tbody>
              <tr v-else><td>{{item.label}}</td><td></td></tr>
            </tbody>
          </table> -->
          <!-- {{editForm.disciplineClass}}<br>
        {{disciplineClassDic}}<br>
        {{disciplineClassDic1}} -->
          <el-form :model="classForm" label-width="100px" ref="classForm" :inline="true">
            <el-form-item label="上级分类" prop="parentClass" :rules="{ required: true, message: '请选择分类', trigger: 'blur' }">
              <el-select v-model="classForm.parentClass" placeholder="请选择">
                <el-option label="一级分类" :value="14"></el-option>
                <el-option v-for="(item, index) in disciplineClassDic1" :key="index" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="分类名称" prop="label" :rules="{ required: true, message: '请输入分类名称', trigger: 'blur' }">
              <el-input v-model="classForm.label" style="width:220px"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="addDic()" type="success">添加</el-button>
            </el-form-item>
          </el-form>
          <el-table :data="diclist" highlight-current-row style="width: 100%;" border class="settingDicTable">
            <!-- <el-table-column prop="value" label="编号">
            </el-table-column>
            <el-table-column prop="dictOrder" label="顺序">
            </el-table-column> -->
            <el-table-column prop="label" label="分类名称">
            </el-table-column>
            <el-table-column label="状态">
              <template slot-scope="scope">
                <span v-html="dicDeleted(scope.row.deleted)"></span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="400">
              <template slot-scope="scope">
                <el-button @click="changeParentDic(scope.row.value)" type="text" icon="el-icon-circle-plus" v-if="!scope.row.isChildren&&!scope.row.deleted">添加二级分类</el-button>
                <el-button @click="setOrder(scope.row.dictOrder,1)" icon="el-icon-arrow-up" type="text" :loading="orderloading">上移</el-button>
                <el-button @click="setOrder(scope.row.dictOrder,2)" icon="el-icon-arrow-down" type="text" :loading="orderloading" style="margin-right:10px;">下移</el-button>
                <el-popconfirm title="确定停用该分类吗？" v-if="!scope.row.deleted" @confirm="stopDic(scope.row.value)">
                  <el-button slot="reference" type="text" icon="el-icon-warning">停用</el-button>
                </el-popconfirm>
                <el-popconfirm title="确定启用该分类吗？" v-if="scope.row.deleted" @confirm="startDic(scope.row.value)">
                  <el-button slot="reference"  type="text" icon="el-icon-success">启用</el-button>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
        </el-dialog>
      </el-form-item>
      <el-form-item label="反映问题分类二其他" prop="otherDisciplineClass" label-width="160px" v-if="otherDisciplineClassVisible">
        <div v-if="readOnly" class="details">{{editForm.otherDisciplineClass}}</div>
        <el-input v-model="editForm.otherDisciplineClass" v-else></el-input>
      </el-form-item>
      <div style="clear:both"></div>
      <el-form-item label="反映主要问题" prop="caseComment" :rules="{ required: true, message: '请输入反映主要问题', trigger: 'blur' }">
        <div v-if="readOnly" class="details" v-html="editForm.caseComment?editForm.caseComment.replace(/\n/g,'<br>'):''"></div>
        <el-input type="textarea" v-model="editForm.caseComment" style="width:965px" v-else></el-input>
      </el-form-item>
      <h1>涉及人员</h1>
      <div v-if="!readOnly">
        <el-form-item label="单位" prop="unit">
          <el-autocomplete style="width:400px" v-model="unit" :fetch-suggestions="querySearch" placeholder="请输入单位名称关键字" :trigger-on-focus="false" ></el-autocomplete>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-autocomplete style="width:250px" v-model="name" :fetch-suggestions="searchPerson" placeholder="请输入姓名" :trigger-on-focus="false" @select="handleSelect">
            <template slot-scope="{ item }">
              {{ item.value }}：{{ item.job }}({{ item.idno }})
            </template>
          </el-autocomplete>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCardNo">
          <el-input v-model="idCardNo" style="width:220px"></el-input>
        </el-form-item>
        <el-form-item label="职务" prop="job">
          <el-input v-model="job" style="width:220px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click.native="addperson">新增</el-button>
        </el-form-item>
      </div>
      <div class="wboxd" style="margin-bottom:20px;overflow:hidden">
        <el-table :data="personlist" highlight-current-row v-loading="listLoading" style="width: 100%;" border>
          <el-table-column type="index" label="序号" width="50" fixed align="center" class-name="tableth">
          </el-table-column>
          <el-table-column prop="unit" label="单位">
          </el-table-column>
          <el-table-column prop="name" label="姓名" width="100">
          </el-table-column>
          <el-table-column prop="idCardNo" label="身份证号" width="200">
          </el-table-column>
          <el-table-column prop="job" label="职务" width="300">
          </el-table-column>
          <el-table-column label="操作" width="300" v-if="!hidePersonAll||!readOnly">
            <template slot-scope="scope">
              <el-button @click="detail(scope.row.idCardNo)" type="primary" style="margin:10px 0;" v-if="!hidePersonAll&&scope.row.idCardNo&&scope.row.idCardNo!==''&&scope.row.idCardNo!==noRoleCode" >查看全方位情况</el-button>
              <el-button type="danger" @click="delperson(scope.$index)" style="margin:10px;" v-if="!readOnly">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div style="clear:both"></div>
      <el-form-item label="批办领导" prop="approveLeader">
        <div v-if="readOnly" class="details">{{editForm.approveLeader}}</div>
        <el-input v-model="editForm.approveLeader" style="width:220px" v-else></el-input>
      </el-form-item>
      <el-form-item label="批办时间" prop="approveDate">
        <div v-if="readOnly" class="details">{{editForm.approveDate}}</div>
        <el-date-picker v-else
          v-model="editForm.approveDate"
          placeholder="请选择登记时间"
          value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="处置方式" prop="approveMethod" >
        <div v-if="readOnly" class="details">{{editForm.approveMethod}}</div>
        <el-input v-model="editForm.approveMethod" style="width:220px" v-else></el-input>
      </el-form-item>
      <div style="clear:both"></div>
      <el-form-item label="查核结论" prop="verifyComment">
        <div v-if="readOnly" class="details" v-html="editForm.verifyComment.replace(/\n/g,'<br>')"></div>
        <el-input type="textarea" v-model="editForm.verifyComment" style="width:965px" v-else></el-input>
      </el-form-item>
      <div style="clear:both"></div>
      <el-form-item label="性质" prop="nature">
        <div v-if="readOnly" class="details">{{editForm.natureText}}</div>
        <el-select v-model="editForm.nature" placeholder="请选择" v-else>
          <el-option v-for="(item, index) in natureDic" :key="index" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <div style="clear:both"></div>
      <el-form-item label="处理方式" prop="processMethod">
        <div v-if="readOnly" class="details">{{editForm.processMethodText}}</div>
        <el-select v-model="editForm.processMethod" placeholder="请选择" v-else>
          <el-option v-for="(item, index) in processMethodDic" :key="index" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="其他处理方式" prop="otherProcessMethod" v-show="editForm.processMethod==='0508'">
        <div v-if="readOnly" class="details">{{editForm.otherProcessMethod}}</div>
        <el-input v-model="editForm.otherProcessMethod" style="width:220px" v-else></el-input>
      </el-form-item>
      <div style="clear:both"></div>
      <el-form-item label="处理决定机关" prop="processOffice">
        <div v-if="readOnly" class="details">{{editForm.processOfficeText}}</div>
        <el-select v-model="editForm.processOffice" placeholder="请选择" v-else>
          <el-option v-for="(item, index) in processOfficeDic" :key="index" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="其他处理决定机关" prop="otherProcessOffice" v-show="editForm.processOffice==='0606'">
        <div v-if="readOnly" class="details">{{editForm.otherProcessOffice}}</div>
        <el-input v-model="editForm.otherProcessOffice" style="width:220px" v-else></el-input>
      </el-form-item>
      <div style="clear:both"></div>
      <el-form-item label="处理情况" prop="processComment">
        <div v-if="readOnly" class="details" v-html="editForm.processComment.replace(/\n/g,'<br>')"></div>
        <el-input type="textarea" v-model="editForm.processComment" style="width:965px" v-else></el-input>
      </el-form-item>
      <div style="clear:both"></div>
      <el-form-item label="办结批准领导" prop="closingLeader">
        <div v-if="readOnly" class="details">{{editForm.closingLeader}}</div>
        <el-input v-model="editForm.closingLeader" style="width:220px" v-else></el-input>
      </el-form-item>
      <el-form-item label="办结时间" prop="closingDate">
        <div v-if="readOnly" class="details">{{editForm.closingDate}}</div>
        <el-date-picker v-else
          v-model="editForm.closingDate"
          placeholder="请选择办结时间"
          value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否需要报送办结结果" label-width="200px" prop="sendRequired">
        <div v-if="readOnly" class="details">{{editForm.sendRequired?'是':'否'}}</div>
        <el-radio-group v-else v-model="editForm.sendRequired">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <div style="clear:both"></div>
      <el-form-item label="当前状态" prop="status">
        <div v-if="readOnly" class="details">{{editForm.statusText}}</div>
        <el-select v-model="editForm.status" placeholder="请选择" v-else>
          <el-option v-for="(item, index) in statusDic" :key="index" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="封数" prop="amount">
        <div v-if="readOnly" class="details">{{editForm.amount}}</div>
        <el-input v-model="editForm.amount" style="width:220px" v-else></el-input>
      </el-form-item>
      <div style="clear:both"></div>
              <el-form-item label="附件" prop="files" v-if="readOnly" >
                <div v-if="!files||files.length===0">无</div>
                <div v-for="(item, index) of files" :key="index">
                  <a :href="$http.defaults.baseURL+$http.defaults.selectionAPI+'api/gen/file/download?id='+item.id">{{item.fileName}}.{{item.fileType}}</a>
                </div>
              </el-form-item>
              <el-form-item label="附件上传" prop="file" v-else>
                <!-- files:{{this.files}}<br>fileList:{{this.fileList}} -->
                  <el-upload
                    class="upload-demo"
                    :action="this.$http.defaults.baseURL+this.$http.defaults.selectionAPI +'api/gen/file/upload/'"
                    :on-success="handleUploadSucc"
                    :on-remove="handleRemove"
                    :before-remove="beforeRemove"
                    :on-error='uploadError'
                    :multiple="true"
                    :file-list="fileList"
                    :before-upload="beforeUpload"
                    accept="application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-works,.wps,.zip,.rar"
                    >
                    <el-button type="primary">点击上传</el-button>
                  </el-upload>
              </el-form-item>
      <div style="clear:both"></div>
      <el-form-item label="备注" prop="comment">
        <div v-if="readOnly" class="details" v-html="editForm.comment.replace(/\n/g,'<br>')"></div>
        <el-input type="textarea" v-model="editForm.comment" style="width:965px" v-else></el-input>
      </el-form-item>
    </el-form>
    <div style="margin:0 140px">
      <el-button type="primary" @click.native="editFormSubmit" :loading="saveloading" v-if="!readOnly">保存</el-button>
      <el-button @click.native="backToList">返回</el-button>
    </div>
  </div>
</template>

<script>
import personInfo from '@/person/Detail.vue'
export default {
  components: {
    personInfo
  },
  watch: {
    data: {
      handler (val) {
        console.log('data:' + val)
        this.getAllDics(true)
        this.getData(val.id)
      },
      immediate: true
    },
    readOnly: {
      handler (val) {
        console.log('data:' + val)
      },
      immediate: true
    },
    timestamp: {
      handler (val) {
        this.formatDicsFromList()
        console.log('timestamp:' + val)
        this.getAllDics(true)
        this.getData(this.data.id)
        if (this.$refs['editForm']) {
          this.$refs['editForm'].resetFields()
        }
      },
      immediate: true
    },
    hidePersonAll: {
      handler (val) {
        console.log('hidePersonAll:' + val)
      },
      immediate: true
    }
  },
  props: {
    listdics: {},
    data: '',
    readOnly: '',
    timestamp: '',
    hidePersonAll: ''
  },
  data () {
    return {
      orderloading: false,
      classForm: {
        parentClass: 14,
        label: ''
      },
      sourceForm: {
        label: ''
      },
      diclist: [],
      diclist1: [],
      settingSourceVisible: false,
      settingVisible: false,
      otherDisciplineClassVisible: false,
      noRoleCode: '***',
      noRoleText: '***',
      pageLoading: false,
      editForm: {
        id: '',
        source: '',
        otherSource: '',
        fileNumber: '',
        registerDate: '',
        title: '',
        caseClass: '',
        disciplineClass: '',
        caseComment: '',
        approveLeader: '',
        approveDate: '',
        approveMethod: '',
        verifyComment: '',
        nature: '',
        processMethod: '',
        otherProcessMethod: '',
        processOffice: '',
        otherProcessOffice: '',
        processComment: '',
        closingLeader: '',
        closingDate: '',
        sendRequired: '',
        status: '',
        amount: '',
        comment: ''
      },
      sourceDic: [],
      natureDic: [],
      caseClassDic: [],
      disciplineClassDic: [],
      disciplineClassDic1: [], // 启用的纪律分类
      statusDic: [],
      processMethodDic: [],
      approveMethodDic: [],
      processOfficeDic: [],
      personlist: [],
      listLoading: false,
      unit: '',
      name: '',
      idCardNo: '',
      job: '',
      otherSourceShow: false,
      otherProcessMethodShow: false,
      otherProcessOfficeShow: false,
      editDataFormLoading: false,
      saveloading: false,
      personDetailVisible: false,
      personId: '',
      files: [],
      fileList: []
    }
  },
  methods: {
    beforeUpload (file) {
      console.log('文件类型')
      console.log(file.name)
      const isFormat = file.name.indexOf('.docx') > 0 || file.name.indexOf('.doc') > 0 || file.name.indexOf('.pdf') > 0 || file.name.indexOf('.DOCX') > 0 || file.name.indexOf('.DOC') > 0 || file.name.indexOf('.PDF') > 0 || file.name.indexOf('.wps') > 0 || file.name.indexOf('.WPS') > 0 || file.name.indexOf('.zip') > 0 || file.name.indexOf('.zip') > 0 || file.name.indexOf('.rar') > 0 || file.name.indexOf('.rar') > 0
      const isLt500M = file.size / 1024 / 1024 < 100
      if (!isLt500M) {
        this.$message.error('单个附件不能超过100M!')
      } else {
      }
      if (!isFormat) {
        this.$message.error('附件格式必须为word、pdf、wps以及压缩包格式文件!')
      } else {
      }
      return isFormat && isLt500M
    },
    getData (id) {
      this.files = []
      this.fileList = []
      this.loading = true
      let url = this.$http.defaults.selectionAPI + 'api/busClueReport/getById?id=' + id
      console.log('id:' + id)
      this.$http.get(url).then(res => {
        this.loading = false
        console.log(res)
        if (res.data.success === true) {
          let rs = res.data.result

          if (rs.files) {
            this.files = rs.files
            this.fileList = this.formatFiles(this.files)
          }
        }
      })
    },
    formatFiles (files) {
      if (files && files.length > 0) {
        let newfiles = []
        for (let i = 0; i < files.length; i++) {
          newfiles.push({name: files[i].fileName + '.' + files[i].fileType, id: files[i].id})
        }
        console.log(newfiles)
        return newfiles
      }
    },
    formatSuccList (fileList) {
      if (fileList && fileList.length > 0) {
        let newfiles = []
        for (let i = 0; i < fileList.length; i++) {
          if (fileList[i].response) {
            newfiles.push({name: fileList[i].response.result[0].fileName + '.' + fileList[i].response.result[0].fileType, id: fileList[i].response.result[0].id})
          } else {
            newfiles.push(fileList[i])
          }
        }
        console.log(newfiles)
        return newfiles
      }
    },
    handleUploadSucc (response, file, fileList) {
      console.log('fileListaddnew:')
      console.log(fileList)
      if (response.success) {
        this.files.push(response.result[0])
        // this.fileList = fileList
        this.fileList = this.formatSuccList(fileList)
        this.$message({
          message: '上传成功！' + response.message,
          type: 'success'
        })
      } else {
        this.$message({
          message: '上传失败:' + response.message,
          type: 'error'
        })
      }
    },
    beforeRemove (file, fileList) {
      if (file && file.status === 'success') {
        return this.$confirm(`确定删除附件“${file.name}”？<br>注意：删除操作实时生效无法恢复`, {
          dangerouslyUseHTMLString: true
        })
      }
    },
    handleRemove (file, fileList) {
      console.log(file)
      let fid = file.id
      let url = this.$http.defaults.selectionAPI + 'api/gen/file/remove'
      this.$http.get(url, {
        params: {
          id: fid
        }
      }).then(res => {
        console.log('删除成功:', res)
        if (res.data.success === true) {
          let newfiles = []
          for (let i = 0; i < this.files.length; i++) {
            if (this.files[i].id !== fid) {
              newfiles.push(this.files[i])
            }
          }
          this.files = newfiles
          this.fileList = this.formatFiles(this.files)
          this.$message({
            message: '删除成功！',
            type: 'success'
          })
        }
      })
    },
    uploadError (err) {
      console.log(err)
      this.$message({
        message: '上传失败:' + err,
        type: 'error'
      })
    },
    formatDicsFromList () {
      this.sourceDic = this.listdics.sourceDic
      if (this.editForm.source && this.sourceDic.filter(item => item.value === this.editForm.source && !item.deleted).length === 0) {
        this.editForm.source = ''
      }
      this.caseClassDic = this.listdics.caseClassDic
      this.disciplineClassDic = this.listdics.disciplineClassDic
      this.init() // 为避免未加载字典造成初始化时无法判断是否选择了“其他”报错，将初始化放到此处
      this.formatDic()
      this.natureDic = this.listdics.natureDic
      this.processMethodDic = this.listdics.processMethodDic
      this.processOfficeDic = this.listdics.processOfficeDic
      this.processOfficeDic = this.listdics.processOfficeDic
    },
    dicDeleted (flag) {
      return flag ? '<font color=red>已停用</font>' : '<font color=green>已启用</font>'
    },
    changeParentDic (val) {
      this.classForm.parentClass = val
    },
    setOrder (dictOrder, moveTo) {
      let para = {dictOrder: dictOrder, moveTo: moveTo}
      this.orderloading = true
      this.$http
        .post(this.$http.defaults.selectionAPI + 'api/sysDict/updateOrder', para)
        .then(response => {
          this.orderloading = false
          if (response.data.success) {
            this.getAllDics()
          } else {
            this.$message({
              message: '调整排序失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          this.orderloading = false
          console.log(error)
          this.$message({
            message: '调整排序网络调用失败:' + error,
            type: 'error'
          })
        })
    },
    startDic (code) {
      this.$http
        .get(this.$http.defaults.selectionAPI + `api/sysDict/enableByCode?dictCode=` + code)
        .then(response => {
          if (response.data.success) {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.getAllDics()
          } else {
            this.$message({
              message: '操作失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(function () {
          this.$message({
            message: '网络操作失败',
            type: 'error'
          })
        })
    },
    stopDic (code) {
      this.$http
        .get(this.$http.defaults.selectionAPI + `api/sysDict/disableByCode?dictCode=` + code)
        .then(response => {
          if (response.data.success) {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.getAllDics()
          } else {
            this.$message({
              message: '操作失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(function () {
          this.$message({
            message: '网络操作失败',
            type: 'error'
          })
        })
    },
    addDic () {
      this.$refs.classForm.validate((valid) => {
        if (valid) {
          let para = {
            dictText: this.classForm.label,
            parentCode: this.classForm.parentClass
          }
          this.$http
            .post(this.$http.defaults.selectionAPI + `api/sysDict/create`, para)
            .then(response => {
              if (response.data.success) {
                this.$message({
                  message: '提交成功',
                  type: 'success'
                })
                this.getAllDics()
                this.classForm.label = ''
              } else {
                this.$message({
                  message: '提交失败:' + response.data.message,
                  type: 'error'
                })
              }
            })
            .catch(function () {
              this.$message({
                message: '网络提交失败',
                type: 'error'
              })
            })
        }
      })
    },
    addDic1 () {
      this.$refs.sourceForm.validate((valid) => {
        if (valid) {
          let para = {
            dictText: this.sourceForm.label,
            parentCode: '01'
          }
          this.$http
            .post(this.$http.defaults.selectionAPI + `api/sysDict/create`, para)
            .then(response => {
              if (response.data.success) {
                this.$message({
                  message: '提交成功',
                  type: 'success'
                })
                this.getAllDics()
                this.sourceForm.label = ''
              } else {
                this.$message({
                  message: '提交失败:' + response.data.message,
                  type: 'error'
                })
              }
            })
            .catch(function () {
              this.$message({
                message: '网络提交失败',
                type: 'error'
              })
            })
        }
      })
    },
    settingSource () {
      this.settingSourceVisible = true
    },
    setting () {
      this.settingVisible = true
    },
    init () {
      this.formatDic()
      if (this.data && this.data.id && this.data.id !== '') {
        this.getPersonList(this.data.id)
        this.editForm = JSON.parse(JSON.stringify(this.data))
        this.disciplineClassChange()
      }
      if (JSON.stringify(this.data) === '{}') {
        this.editFormReset()
      }
    },
    formatDic () {
      this.diclist = []
      this.disciplineClassDic1 = []
      for (let item of this.disciplineClassDic) {
        let itemnew = JSON.parse(JSON.stringify(item))
        let noChildren = true
        if (item.children) {
          for (let it of item.children) {
            if (it.deleted) {
              noChildren = false
              break
            }
          }
        }
        this.diclist.push({label: item.label, value: item.value, dictOrder: item.dictOrder, isChildren: false, noChildren: noChildren, deleted: item.deleted})
        if (item.children) {
          itemnew.children = []
          for (let it of item.children) {
            this.diclist.push({label: '—— ' + it.label, value: it.value, dictOrder: it.dictOrder, isChildren: true, noChildren: true, deleted: it.deleted})
            if (it.deleted === 0) {
              itemnew.children.push(it)
            }
          }
          if (itemnew.children.length === 0) {
            itemnew.children = null
          }
        }
        if (itemnew.deleted === 0) {
          this.disciplineClassDic1.push(itemnew)
        }
      }
    },
    disciplineClassChange () {
      if (this.editForm.disciplineClass !== '***') {
        let flag = false
        for (let item of this.editForm.disciplineClass) {
          for (let it of item) {
            if (this.disciplineClass2Name(it).indexOf('其他') > -1) {
              // if (it === '140105') {
              flag = true
            }
          }
        }
        this.otherDisciplineClassVisible = flag
      }
    },
    disciplineClassName (arr, othertext) {
      if (arr !== '***') {
      // if (arr === this.noRoleCode) { return this.noRoleText }
        let label = ''
        for (let item of arr) {
          if (item !== arr[0]) {
            label = label + '、'
          }
          for (let it of item) {
            if (it === item[0]) {
              label = label + this.disciplineClass2Name(it, othertext)
            } else {
              label = label + ' > ' + this.disciplineClass2Name(it, othertext)
            }
          }
        }
        return label
      } else {
        return '***'
      }
    },
    disciplineClass2Name (code, othertext) {
      for (let item of this.disciplineClassDic) {
        if (item.value === code) {
          if (item.label === '其他' && othertext && othertext !== '') {
            return item.label + '（' + othertext + '）'
          } else {
            return item.label
          }
        }
        if (item.children) {
          for (let it of item.children) {
            if (it.value === code) {
              if (it.label === '其他' && othertext && othertext !== '') {
                return it.label + '（' + othertext + '）'
              } else {
                return it.label
              }
            }
          }
        }
      }
      return code
    },
    backToList () {
      this.$emit('dialogHide', false)
    },
    persondetail (id) {
      this.personDetailVisible = true
      this.personId = id
    },
    detail (idcardNo) {
      this.$http
        .get(this.$http.defaults.selectionAPI + `api/selection/jiben/getIdByIdCardNo?idcard=` + idcardNo)
        .then(response => {
          if (response.data.success) {
            if (response.data.result) {
              this.persondetail(response.data.result)
            } else {
              this.$message({
                message: '无此人选的干部全方位情况',
                type: 'error'
              })
            }
          } else {
            this.$message({
              message: '查询失败:无此人选的干部全方位情况',
              type: 'error'
            })
          }
        })
        .catch(function () {
          this.$message({
            message: '查询失败:无此人选的干部全方位情况',
            type: 'error'
          })
        })
    },
    delperson (index) {
      console.log('index:' + index)
      this.personlist.splice(index, 1)
    },
    addperson () {
      if (!this.unit || this.unit === '') {
        this.$message({
          message: '请填写涉及人员单位',
          type: 'error'
        })
        return
      }
      if (!this.name || this.name === '') {
        this.$message({
          message: '请填写人员姓名',
          type: 'error'
        })
        return
      }
      // if (!this.idCardNo || this.idCardNo === '') {
      //   this.$message({
      //     message: '请填写身份证号',
      //     type: 'error'
      //   })
      //   return
      // }
      if (!this.job || this.job === '') {
        this.$message({
          message: '请填写职务',
          type: 'error'
        })
        return
      }
      this.personlist.push({unit: this.unit, name: this.name, job: this.job, idCardNo: this.idCardNo})
      // this.unit = ''
      this.name = ''
      this.job = ''
      this.idCardNo = ''
    },
    handleSelect (item) {
      this.idCardNo = item.idno
      this.job = item.job
    },
    querySearch (queryString, cb) {
      return this.getunits(this, queryString, cb)
    },
    getunits (self, queryString, cb) {
      let para = {
        unitname: queryString
      }
      self.$http
        .post(`/api/prj/searchunit/`, para)
        .then(function (response) {
          console.log('获取成功')
          var results = response.data
          let newArr = []
          results.forEach(function (item) {
            let obj = {'value': item}
            newArr.push(obj)
          })
          cb(newArr)
        })
        .catch(function (error) {
          console.log(error)
        })
    },
    searchPerson (queryString, cb) {
      return this.getpersons(this, queryString, cb, this.unit)
    },
    getpersons (self, queryString, cb, unitname) {
      let para = {
        realname: queryString,
        unitname: unitname
      }
      console.log(para)
      self.$http
        .get(this.$http.defaults.selectionAPI + `api/selection/jiben`, {params: para})
        .then(function (response) {
          console.log('获取成功')
          var results = response.data.result
          let newArr = []
          results.forEach(function (item) {
            let obj = {'value': item.a0101, 'idno': item.a0184, 'job': item.a0201a + ' ' + item.a0215a}
            newArr.push(obj)
          })
          cb(newArr)
        })
        .catch(function (error) {
          console.log(error)
        })
    },
    getDics (code, initflag) {
      this.$http
        .get(this.$http.defaults.selectionAPI + `api/sysDict/getByCode?code=` + code)
        .then(response => {
          if (response.data.success) {
            switch (code) {
              case '01':
                this.sourceDic = response.data.result
                console.log(this.editForm.source)
                if (this.editForm.source && this.sourceDic.filter(item => item.value === this.editForm.source && !item.deleted).length === 0) {
                  this.editForm.source = ''
                }
                break
              case '02':
                this.caseClassDic = response.data.result
                break
              case '14':
                this.disciplineClassDic = response.data.result
                // if (this.editForm.disciplineClass && this.disciplineClassDic.filter(item => item.value === this.editForm.disciplineClass && !item.deleted).length === 0) {
                //   this.editForm.disciplineClass = ''
                // }
                if (initflag) {
                  this.init() // 为避免未加载字典造成初始化时无法判断是否选择了“其他”报错，将初始化放到此处
                }
                this.formatDic()
                break
              case '04':
                this.natureDic = response.data.result
                break
              case '05':
                this.processMethodDic = response.data.result
                break
              case '06':
                this.processOfficeDic = response.data.result
                break
              case '07':
                this.statusDic = response.data.result
                break
            }
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
        })
    },
    getAllDics (initflag) {
      this.getDics('01', initflag)
      this.getDics('02', initflag)
      this.getDics('14', initflag)
      this.getDics('04', initflag)
      this.getDics('05', initflag)
      this.getDics('06', initflag)
      this.getDics('07', initflag)
    },
    editFormReset () {
      if (this.$refs['editForm']) {
        this.$refs['editForm'].resetFields()
      }
      this.editForm = {
        source: '',
        otherSource: '',
        fileNumber: '',
        registerDate: '',
        title: '',
        caseClass: '',
        disciplineClass: '',
        caseComment: '',
        approveLeader: '',
        approveDate: '',
        approveMethod: '',
        verifyComment: '',
        nature: '',
        processMethod: '',
        otherProcessMethod: '',
        processOffice: '',
        otherProcessOffice: '',
        processComment: '',
        closingLeader: '',
        closingDate: '',
        sendRequired: '',
        status: '',
        amount: '',
        comment: ''
      }
      this.personlist = []
      this.unit = ''
      this.name = ''
      this.job = ''
      this.idCardNo = ''
    },
    editFormSubmit () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          let para = {
            id: this.editForm.id,
            source: this.editForm.source,
            otherSource: this.editForm.otherSource,
            fileNumber: this.editForm.fileNumber,
            registerDate: this.editForm.registerDate,
            title: this.editForm.title,
            caseClass: this.editForm.caseClass,
            // disciplineClass: Array.isArray(this.editForm.disciplineClass) ? this.editForm.disciplineClass[this.editForm.disciplineClass.length - 1] : this.editForm.disciplineClass,
            disciplineClass: JSON.stringify(this.editForm.disciplineClass),
            otherDisciplineClass: this.editForm.otherDisciplineClass,
            caseComment: this.editForm.caseComment,
            approveLeader: this.editForm.approveLeader,
            approveDate: this.editForm.approveDate,
            approveMethod: this.editForm.approveMethod,
            verifyComment: this.editForm.verifyComment,
            nature: this.editForm.nature,
            processMethod: this.editForm.processMethod,
            otherProcessMethod: this.editForm.otherProcessMethod,
            processOffice: this.editForm.processOffice,
            otherProcessOffice: this.editForm.otherProcessOffice,
            processComment: this.editForm.processComment,
            closingLeader: this.editForm.closingLeader,
            closingDate: this.editForm.closingDate,
            sendRequired: this.editForm.sendRequired,
            status: this.editForm.status,
            amount: this.editForm.amount,
            comment: this.editForm.comment,
            persons: this.personlist,
            files: this.files.map(item => item.id)
          }
          this.saveloading = true
          this.$http
            .post(this.$http.defaults.selectionAPI + `api/busClueReport/saveReport`, para)
            .then(response => {
              this.saveloading = false
              if (response.data.success) {
                this.$message({
                  message: '保存成功',
                  type: 'success'
                })
                this.editFormReset()
                this.$emit('dialogHide', true)
              } else {
                this.$message({
                  message: '保存失败:' + response.data.message,
                  type: 'error'
                })
              }
            })
            .catch(error => {
              this.saveloading = false
              console.log(error)
              this.$message({
                message: '网络保存失败:' + error,
                type: 'error'
              })
            })
        }
      })
    },
    setdata (data) {
      this.preferments.forEach((item, index) => {
        this.preferments[index].inname = ''
        this.preferments[index].inchusheng = ''
        this.preferments[index].inyuanzhiwu = ''
        this.preferments[index].inxianzhiwu = ''
        this.preferments[index].inrenzhishijian = ''
      })
      this.preferments.forEach((item, index) => {
        if (data.length >= index + 1) {
          this.preferments[index].inname = data[index].Name
          this.preferments[index].inchusheng = data[index].Chusheng
          this.preferments[index].inyuanzhiwu = data[index].Yuanzhiwu
          this.preferments[index].inxianzhiwu = data[index].Xianzhiwu
          this.preferments[index].inrenzhishijian = data[index].Shijian
        }
      })
    },
    // 获取列表
    getImportList (res) {
      if (res.success) {
        this.$message({
          message: '导入成功！',
          type: 'success'
        })
        this.setdata(res.content)
      } else {
        console.log(res.Msg)
        this.$message({
          message: '导入失败:' + res.message,
          type: 'error'
        })
      }
      this.fileList = []
    },
    exportticket () {
      window.open(this.$http.defaults.baseURL + 'api/prj/wordfile/' + this.myid)
    },
    str2zhiwu (str) {
      if (str === '正职') {
        str = '正职（主要）领导'
      }
      if (str === '副职') {
        str = '副职领导'
      }
      return str
    },
    initdata () {
      let myflag = false
      for (let index in this.leaders) {
        if (this.leaders[index].inidnumber !== '' || this.leaders[index].inname !== '' || this.leaders[index].inzhiwu !== '') {
          myflag = true
          break
        }
      }
      if (myflag) {
        this.$confirm('确定要覆盖当前填写的班子成员数据吗', '提示', {
          type: 'success'
        }).then(() => {
          this.setperson()
        })
      } else {
        this.setperson()
      }
    },
    setperson () {
      this.leaders.forEach((item, index) => {
        this.leaders[index].inidnumber = ''
        this.leaders[index].inname = ''
        this.leaders[index].inzhiwu = ''
      })
      let para = {
        unitname: this.editUnitForm.unitname
      }
      let _this = this
      this.$http
        .post(`/api/prj/initdata/`, para)
        .then(function (response) {
          console.log('获取成功')
          var results = response.data
          console.log('results.length:' + results.length)
          if (results === null || results !== []) {
            _this.leaders.forEach((item, index) => {
              if (results.length >= index + 1) {
                _this.leaders[index].inidnumber = results[index].A0184
                _this.leaders[index].inname = results[index].A0101
                _this.leaders[index].inzhiwu = results[index].A0215A
              }
            })
          }
        })
        .catch(function (error) {
          console.log(error)
        })
    },
    beforeAvatarUpload (file) {
      console.log('文件类型')
      console.log(file.name)
      const isXLS = file.name.indexOf('.xls') > 0
      const isXLSX = file.name.indexOf('.xlsx') > 0
      // const isLt5M = file.size / 1024 / 1024 < 500
      if (!isXLS && !isXLSX) {
        this.$message.error('上传文件只能是 xls/xlsx 格式!')
      }
      // if (!isLt5M) {
      //   this.$message.error('上传文件大小不能超过 500MB!')
      // }
      // return isZIP && isLt5M
      return isXLS || isXLSX
    },
    handlePreview (file) {
      console.log(file)
    },
    handleExceed (files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，如有多个文件请压缩为zip文件打包上传。`)
    },
    handleCurrentChange (val) {
      this.pageNum = val
      this.getImportList()
    },
    importPersons () {
      this.getImportList()
    },
    uploaderror (err) {
      console.log('具体错误')
      console.log(err)
      this.$message({
        message: '导入失败，可能是文件大小超出系统限制:' + err,
        type: 'error'
      })
    },
    datadownload (id) {
      window.open(this.$http.defaults.baseURL + 'admin/binfo/download')
    },
    getPersonList (id) {
      this.listLoading = true
      this.$http
        .get(this.$http.defaults.selectionAPI + `api/busClueReport/queryPersonsByReport?clueReportId=` + id)
        .then(response => {
          this.listLoading = false
          if (response.data.success) {
            this.personlist = response.data.result
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          this.listLoading = false
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
        })
    }
  }
}
</script>
<style>
  #cluereportedit h1{
    font-size:var(--size16) !important;
    font-weight: bolder;
    height: 30px;
    border-bottom: 1px solid #95B8E7;
    font-family: '宋体';
    line-height: 30px;
    color: #000;
    padding-left:10px;
    background-image: linear-gradient(to bottom , #EEF4FF, #E0ECFF);
    margin:0 0 20px;
  }
  #cluereportedit .el-table{
    border: #95B8E7 solid 1px;
  }
  .settingDicTable th{
    font-weight: normal;
    background-image: -webkit-gradient(linear , left top, left bottom , from(#f8f8f8), to(#efefef));
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
  }
  .settingDicTable td,
  .settingDicTable th{
    height: 42px;
    padding:0 !important;
  }
</style>
<style scoped>
.details{
  font-weight:bolder;
  border-bottom:1px solid #95B8E7;
  padding:0 10px;
  min-height: 36px
}
</style>
